package com.gongpingjialoan.activity.car;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.adapter.DomainAdapter;
import com.gongpingjialoan.bean.Domain;
import com.gongpingjialoan.network.NetDataJson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2016/10/9.
 */
public class DomainActivity extends BaseActivity implements AdapterView.OnItemClickListener, OnClickListener {

    private NetDataJson mNetDataJson;

    private ListView mListView;

    private TextView sureTextView;

    private ArrayList<Domain> mDomains;

    private ArrayList<Domain> selectDomains;

    private DomainAdapter mDomainAdapter;

    private String domainString;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.domain_layout);
        setTitle("选择渠道");
        mListView = (ListView) findViewById(R.id.list_view);
        sureTextView = (TextView) findViewById(R.id.right_title);
        sureTextView.setText("确定");
        sureTextView.setVisibility(View.VISIBLE);
        sureTextView.setOnClickListener(this);
        mListView.setOnItemClickListener(this);
        selectDomains = new ArrayList<>();
        domainString = getIntent().getStringExtra("domainString");
        mDomains = new ArrayList<>();
        mDomainAdapter = new DomainAdapter(mDomains, this);
        mListView.setAdapter(mDomainAdapter);
        getData();
    }

    private void getData() {
        mNetDataJson = new NetDataJson(new NetDataJson.OnNetDataJsonListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                hidenProgressDialog();
                Toast.makeText(DomainActivity.this, errorMessage, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDataJsonUpdate(JSONObject json) {
                hidenProgressDialog();
                JSONArray jsonArray;
                try {
                    jsonArray = json.getJSONArray("domain_list");
                    int size = jsonArray.length();
                    for (int i = 0; i < size; i++) {
                        JSONArray array = jsonArray.getJSONArray(i);
                        Domain domain = new Domain();
                        domain.setName(array.getString(1));
                        domain.setValue(array.getString(0));
                        if(domainString.contains(domain.getName())){
                            domain.setIsCheck(true);
                            selectDomains.add(domain);
                        }else {
                            domain.setIsCheck(false);
                        }
                        mDomains.add(domain);
                    }
                    mDomainAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                }
            }
        });
        mNetDataJson.setUrl("/mobile/api/get-all-domain-list/");
        mNetDataJson.request("get");
        showProgressDialog("加载中...");

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Domain domain = mDomains.get(position);
        if(!domain.isCheck()){
            selectDomains.add(domain);
            domain.setIsCheck(true);
        }else{
            selectDomains.remove(domain);
            domain.setIsCheck(false);
        }
        mDomainAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra("Domain", selectDomains);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }
}
